/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "DownloadGroupFriend.h"

/*
 * =====================================================================================
 *        Class:  DownloadGroupFriendPacket
 *  Description:  请求下载好友分组列表的请求包 
 * =====================================================================================
 */

int DownloadGroupFriendPacket::putBody(uint8_t* buf) const {
    uint8_t* cur = buf;
    // 操作字节
    cur += writeBufB(cur, 0x01);
    // 未知字节0x2，未知的4字节0
    cur += writeBufB(cur, 0x02);
    cur += writeBufDw(cur, 0);
    cur += writeBufDw(cur, beginFrom);

    return cur - buf;
}

/*
 * =====================================================================================
 *        Class:  DownloadGroupFriendReplyPacket
 *  Description:  请求下载好友分组列表的回复包 
 * =====================================================================================
 */

DownloadGroupFriendReplyPacket::
DownloadGroupFriendReplyPacket(const uint8_t* buf, int len) :
    InPacket(buf, len, true)
{

    const uint8_t* cur = buf + QQ_HEADER_LENGTH_BASIC_FAMILY_IN;

    // 操作字节，下载为0x1
    cur += readBufB(subCommand, cur);
    // 回复码，1字节
    cur += readBufB(replyCode, cur);
    // 4个未知字节，全0
    cur += 4;
    // 起始好友号
    cur += readBufDw(beginFrom, cur);

    const uint8_t* max = buf + len;
    members.reserve((max - cur) / sizeof (QQGroupFriend) + 1);

    // 循环读取各好友信息，加入到list中
    while (cur < max) {
	members.push_back(QQGroupFriend(cur, max - cur));
	cur += 6;
    }
    if (cur - buf > len)
	throw runtime_error("DownloadGroupFriendReplyPacket() Error: BUFFER len not enough");
}


/*
 * =====================================================================================
 *        Class:  GroupDataOpPacket
 *  Description:  上传下载分组名的请求包
 * =====================================================================================
 */

int GroupDataOpPacket::putBody(uint8_t* buf) const
{
    uint8_t* cur = buf;

    cur += writeBufB(cur, type);
    if (QQ_UPLOAD_GROUP_NAME) {
	Members::const_iterator first = members.begin();
	Members::const_iterator last  = members.end();
	for (; first != last; ++first) {
	    cur += first->putBody(cur);
	}
    } else {
	cur += writeBufB(cur, 0x02);
	cur += writeBufDw(cur, 0x00000000);
    }

    return cur - buf;
}
/*
 * =====================================================================================
 *        Class:  GroupDataOpReplyPacke
 *  Description:  下载分组名的回复包
 * =====================================================================================
 */


GroupDataOpReplyPacket::GroupDataOpReplyPacket(const uint8_t* buf, int len):
    InPacket(buf, len, true)
{
    const uint8_t* cur = buf + QQ_HEADER_LENGTH_BASIC_FAMILY_IN;

    cur += readBufB(subCommand, cur);
    uint8_t replyCode;
    cur += readBufB(replyCode, cur);

    if ((QQ_GROUP_DATA_OP_OK == replyCode) &&
	    (QQ_DOWNLOAD_GROUP_NAME == subCommand)) {
	cur += 4; // 未知4个字节

	const uint8_t* max = buf + len;

	members.reserve((max - cur) / 17 + 1);
	while (cur < max) {
	    members.push_back(QQGroupName(cur));
	    cur += 17;
	}
    }
    if (cur - buf > len)
	throw runtime_error("GroupDataOpReplyPacke() Error: BUFFER len not enough");
}

